package com.example.demo.service.impl;

import com.example.demo.model.Dishes;
import com.example.demo.model.Ingredients;
import com.example.demo.model.User;
import com.example.demo.repository.AuthRepository;
import com.example.demo.service.RecommendationService;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RecommendationServiceImpl implements RecommendationService {
    private final AuthRepository userRepository;

    public RecommendationServiceImpl(AuthRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public List<Ingredients> getRecommendedIngredients(Long userId) {
        User user = userRepository.findByUserId(userId);
        // 根据用户的食材偏好或其他要求生成推荐的食材列表
        List<Ingredients> recommendedIngredients = new ArrayList<>();

        if(user.getDishes()!=null){
            List<Dishes> userDishes = user.getDishes();
            Map<Ingredients, Integer> ingredientFrequencyMap = new HashMap<>();

            // 统计食材的出现次数
            for(Dishes dish : userDishes){
                List<Ingredients> dishIngredients = dish.getDIngredients();
                for(Ingredients ingredient : dishIngredients){
                    ingredientFrequencyMap.put(ingredient, ingredientFrequencyMap.getOrDefault(ingredient, 0) + 1);
                }
            }

            // 对食材按照出现频率进行排序
            List<Map.Entry<Ingredients, Integer>> sortedIngredients = new ArrayList<>(ingredientFrequencyMap.entrySet());
            sortedIngredients.sort((entry1, entry2) -> entry2.getValue().compareTo(entry1.getValue()));

            // 选出出现频率最高的三种食材
            int count = 0;
            for(Map.Entry<Ingredients, Integer> entry : sortedIngredients){
                recommendedIngredients.add(entry.getKey());
                count++;
                if(count >= 3){
                    break;
                }
            }
        } else {
            // 如果用户没有制作过菜品，则从浏览过的菜品中选择食材进行推荐
            List<Dishes> viewedDishes = user.getViewedDishes();
            Map<Ingredients, Integer> viewedIngredientFrequencyMap = new HashMap<>();
            // 统计浏览过的菜品中食材的出现次数
            for(Dishes dish : viewedDishes){
                List<Ingredients> dishIngredients = dish.getDIngredients();
                for(Ingredients ingredient : dishIngredients){
                    viewedIngredientFrequencyMap.put(ingredient, viewedIngredientFrequencyMap.getOrDefault(ingredient, 0) + 1);
                }
            }

            // 对食材按照出现频率进行排序
            List<Map.Entry<Ingredients, Integer>> sortedViewedIngredients = new ArrayList<>(viewedIngredientFrequencyMap.entrySet());
            sortedViewedIngredients.sort((entry1, entry2) -> entry2.getValue().compareTo(entry1.getValue()));

            // 选出浏览过菜品中出现频率最高的三种食材
            int count = 0;
            for(Map.Entry<Ingredients, Integer> entry : sortedViewedIngredients){
                recommendedIngredients.add(entry.getKey());
                count++;
                if(count >= 3){
                    break;
                }
            }
        }
        return recommendedIngredients;
    }

    @Override
    public List<Dishes> getRecommendedFood(Long userId) {
        User user = userRepository.findByUserId(userId);
        List<Dishes> recommendedFood = new ArrayList<>();
        if(user.getDishes()!=null){
            List<Dishes> userDishes = user.getDishes();
            List<Dishes> madeDishes = new ArrayList<>();
            List<Dishes> viewedDishes = new ArrayList<>();
            // 分别将用户制作的菜品和浏览过的菜品进行分类
            for(Dishes dish : userDishes){
                if(dish.getViewCount() > 0){
                    viewedDishes.add(dish);
                } else {
                    madeDishes.add(dish);
                }
            }
            // 优先从用户制作的菜品中选择
            Random rand = new Random();
            while(madeDishes.size() > 0 && recommendedFood.size() < 3){
                int index = rand.nextInt(madeDishes.size());
                recommendedFood.add(madeDishes.get(index));
                madeDishes.remove(index);
            }
            // 如果用户制作的菜品不足3种，从浏览过的菜品中选择
            while(viewedDishes.size() > 0 && recommendedFood.size() < 3){
                int index = rand.nextInt(viewedDishes.size());
                recommendedFood.add(viewedDishes.get(index));
                viewedDishes.remove(index);
            }
        }
        return recommendedFood;
    }

}
